/* 
   Copyright 2012 Ryne Okimoto

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

package panels;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import recipe.Ingredient;

@SuppressWarnings("serial")
public class IngredientPanel extends JPanel{

	private JScrollPane scrollPane;
	private JButton btnAdd;
	private JLabel lblAmount;
	private JLabel lblUnit;
	private JLabel lblName;
	private JPanel rowPanel;
	private ArrayList<IngredientRow> listOfRows;
	private int numberOfRows;
	
	/**
	 * Constructor
	 */
	public IngredientPanel(){
		// Set layout
		this.setLayout(new GridBagLayout());
		
		// Initialize components
		numberOfRows = 0;
		
		btnAdd = new JButton("Add ingredient");
		btnAdd.addActionListener(new AddClickedListener());
		
		lblAmount = new JLabel("Amount");
		lblUnit = new JLabel("Unit");
		lblName = new JLabel("Name");
		listOfRows = new ArrayList<IngredientRow>();
		
		JPanel labels = new JPanel();
		labels.add(lblAmount);
		labels.add(lblUnit);
		labels.add(lblName);
		GridBagConstraints lblCon = new GridBagConstraints();
		lblCon.fill = GridBagConstraints.HORIZONTAL;
		lblCon.gridx = 0;
		lblCon.gridy = 1;
		
		// Add components to viewport
		rowPanel = new JPanel(new GridBagLayout());
		
		// Add viewport to scrollpane
		scrollPane = new JScrollPane(rowPanel, 
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setPreferredSize(new Dimension(600, 200));
		
		GridBagConstraints btnCon = new GridBagConstraints();
		btnCon.anchor = GridBagConstraints.PAGE_START;
		
		GridBagConstraints scrollCon = new GridBagConstraints();
		scrollCon.gridwidth = GridBagConstraints.REMAINDER;
		scrollCon.gridx = 0;
		scrollCon.gridy = 2;
		
		this.add(btnAdd, btnCon);
		this.add(labels, lblCon);
		this.add(scrollPane, scrollCon);
	}
	
	/**
	 * Method - setRows
	 * Description - Initializes all rows with a ArrayList of ingredients
	 * @param ingList
	 * @return void
	 */
	public void setRows(ArrayList<Ingredient> ingList){
		int listSize = ingList.size();
		IngredientRow tempRow = null;
		for(int i = 0; i < listSize; i++){
			tempRow = initRow(ingList.get(i));
			// Add to list
			listOfRows.add(tempRow);
			// Add to viewport
			rowPanel.add(tempRow, createConstraints(numberOfRows, 0));
			numberOfRows++;
		}
		
		this.revalidate();
	}
	
	private IngredientRow initRow(Ingredient i){
		IngredientRow r = new IngredientRow(this, scrollPane, rowPanel, listOfRows);
		r.setIngredient(i);
		return r;
	}
	
	/**
	 * Method - decrementRowCount
	 * Description - Decrements the number of rows
	 * @return True - Returns if operation completes successfully
	 * @return False - Returns if operation does not complete
	 */
	public boolean decrementRowCount(){
		boolean isCountDecreased = false;
		if(numberOfRows > 0){
			numberOfRows--;
			isCountDecreased = true;
		}
		
		return isCountDecreased;
	}
	
	public String getIngredientString(){
		StringBuilder temp = new StringBuilder();
		for(int i = 0; i < listOfRows.size(); i++){
			temp.append(listOfRows.get(i).getIngredientAsString());
			if(!(i == listOfRows.size() - 1)){
				temp.append("\n");
			}
		}
		
		return temp.toString();
	}
	
	/**
	 * Class - AddClickedListener
	 * Description - Listener class for the add button
	 * @author Ryne
	 *
	 */
	private class AddClickedListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			// Create row
			IngredientRow row = new IngredientRow(
					IngredientPanel.this, 
					IngredientPanel.this.scrollPane,
					IngredientPanel.this.rowPanel,
					IngredientPanel.this.listOfRows);
			// Add to viewport
			rowPanel.add(row, createConstraints(numberOfRows++, 0));
			// Add to list
			listOfRows.add(row);
			// Refresh view
			IngredientPanel.this.validate();
		}
	}
	
	/**
	 * Method - createConstraints
	 * Description - Returns GridBagConstraints with specified position
	 * @param row
	 * @param col
	 * @return GridBagConstraints
	 */
	private GridBagConstraints createConstraints(int row, int col){
		GridBagConstraints con = new GridBagConstraints();
		
		con.gridx = col;
		con.gridy = row;
		con.insets = new Insets(10, 10, 10, 10);
		
		return con;
	}
	
	/**
	 * Unit tester
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame view = new JFrame();
		IngredientPanel pane = new IngredientPanel();
		
		view.setContentPane(pane);
		
		view.pack();
		
		view.setVisible(true);
	}
}
